package ke07.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class UDPClient {
	
	public static void main(String[] args) {
		
		byte[] sendData = new byte[1024]; 
		byte[] receiveData = new byte[1024];
		
		Scanner s = new Scanner(System.in);
		System.out.print("Bitte geben sie einen Satz ein: ");
		String toSend = s.nextLine();
		
		DatagramSocket clientSocket = null;
		try {
			clientSocket = new DatagramSocket();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		
		sendData = toSend.getBytes();
		
		InetAddress serverIP = null;
		try {
			// serverIP = InetAddress.getLocalHost();
			serverIP = InetAddress.getByName("localhost");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DatagramPacket sendPacket = new DatagramPacket(sendData,
				sendData.length, serverIP, 9876);
		
		try {
			clientSocket.send(sendPacket);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Nachricht an Server gesendet!");
				
		DatagramPacket receivePacket = new DatagramPacket(receiveData,
				receiveData.length); 
		
		try {
			clientSocket.receive(receivePacket);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String modifiedSentence = new String(receivePacket.getData());
		
		clientSocket.close();
		
		System.out.println("Nachricht erhalten: "+modifiedSentence);
	}
}
